package ua.org.cityid.client.mvp;

import java.util.ArrayList;
import java.util.List;

import ua.org.cityid.client.controls.NavBar;
import ua.org.cityid.client.controls.RegionAnchor;
import ua.org.cityid.shared.data.City;
import ua.org.cityid.shared.data.Regions;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.RecordList;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;

public class SmartView extends Composite implements MainPresenter.Display{

	final ListGrid details = new ListGrid();
	ListGridField fieldId = new ListGridField("CITY_ID");
	ListGridField fieldName = new ListGridField("CITY_NAME");
	ListGridField fieldPhone = new ListGridField("CITY_PHONE");
	
	private static smartPageUiBinder uiBinder = GWT
	.create(smartPageUiBinder.class);

	interface smartPageUiBinder extends UiBinder<Widget, SmartView> {
	}
	
	@UiField
	Grid regionsList;
	@UiField
	FlowPanel panel;
	@UiField
	NavBar navBar;
	
	private List<RegionAnchor> regionAnchors = new ArrayList<RegionAnchor>();
	
	@Inject
	public SmartView(){
		initWidget(uiBinder.createAndBindUi(this));
		setRegions();
		details.setWidth(800);
		details.setHeight(400);
		fieldId.setTitle("ID");
		fieldName.setTitle("Город");
		fieldPhone.setTitle("Телефонный код");
		details.setFields(fieldId, fieldName, fieldPhone);
		panel.add(((Canvas)details));
	}
	
	private void setRegions(){
		regionsList.resize(5, 5);
		for (int index = 0; index < Regions.list.length; index++){
			RegionAnchor a = new RegionAnchor(index, Regions.list[index]);
			regionsList.setWidget(index / 5,  index % 5 , a);
			regionAnchors.add(a);
		}
	}
	
	@Override
	public void fillCitiesTable(List<City> cities) {
		RecordList list = details.getRecordList();
		for (Record r : list.toArray()) {
			details.removeData(r);	
		}
		
		for (City c : cities){
			Record r = new Record();
			r.setAttribute("CITY_ID", c.getId());
			r.setAttribute("CITY_NAME", c.getName());
			r.setAttribute("CITY_PHONE", c.getPhoneCode());
			
			details.addData(r);
		}
		
	}
	@Override
	public HasClickHandlers getNavBarNext() {
		return navBar.getButtonNext();
	}
	
	@Override
	public HasClickHandlers getNavBarPrev() {
		return navBar.getButtonPrev();
	}

	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public List<RegionAnchor> getRegionAnchors() {
		return regionAnchors;
	}

	@Override
	public void updateNavBar(int itemsTotal, int startIndex) {
		navBar.update(startIndex, itemsTotal, MainPresenter.MAX_VISIBLE_REGIONS);
		
	}

	@Override
	public void startProcessing() {
	}

	@Override
	public void stopProcessing() {
	}

}
